//
//  MTHomePlayControlView.swift
//  MT
//
//  Created by ooops on 2020/6/2.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import ZFPlayer

class MTHomePlayControlView: UIView, ZFPlayerMediaControl {
    var player: ZFPlayerController?
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        makeConstraint()
    }
    
    func resetControlView() {
        self.coverImageView.image = nil
        self.pauseIconButtonLeft.isHidden = true
    }
    
    func show(cover: String?, imageMode: UIView.ContentMode) {
        self.coverImageView.contentMode = imageMode
        self.coverImageView.mtSetImage(cover, nil)
    }
    
    func videoPlayer(_ videoPlayer: ZFPlayerController, loadStateChanged state: ZFPlayerLoadState) {
        if state == .prepare {
            self.coverImageView.isHidden = false
        } else if state == .playthroughOK || state == .playable {
            self.coverImageView.isHidden = true
        }
        if (state == .stalled || state == .prepare) && (videoPlayer.currentPlayerManager.isPlaying == true) {
            
        }
    }
    
    func gestureSingleTapped(_ gestureControl: ZFPlayerGestureControl) {
        if self.player?.currentPlayerManager.isPlaying == true {
            self.player?.currentPlayerManager.pause?()
            self.pauseIconButtonLeft.isHidden = false
            self.pauseIconButtonLeft.transform = CGAffineTransform.init(scaleX: 1.5, y: 1.5)
            UIView.animate(withDuration: 0.2, delay: 0, options: UIView.AnimationOptions.curveEaseIn, animations: {
                self.pauseIconButtonLeft.transform = .identity
            }) { (flag) in
                
            }
        } else {
            self.player?.currentPlayerManager.play?()
            self.pauseIconButtonLeft.isHidden = true
        }
    }
    
    /// 视频暂停 / 开始按钮
    lazy var pauseIconButtonLeft: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "icon_stop-1"), for: .normal)
        btn.isUserInteractionEnabled = false
        btn.isHidden = true
        return btn
    }()
    
    lazy var coverImageView: UIImageView = {
        let imgView = UIImageView()
        imgView.contentMode = .scaleAspectFill
        imgView.isUserInteractionEnabled = true
        imgView.clipsToBounds = true
        imgView.tag = 100
        return imgView
    }()
    
    private func setupUI() {
        addSubview(coverImageView)
        addSubview(pauseIconButtonLeft)
    }
    
    private func makeConstraint() {
        pauseIconButtonLeft.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.size.equalTo(CGSize(width: 40, height: 40))
        }
        
        coverImageView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }


}
